<?php

namespace app\beautiful\controller\admin\setting;

use app\beautiful\model\StaffModel;
use app\BaseController;
use app\common\logic\DataLogic;
use app\common\model\CustomerServiceModel;
use app\common\model\ShopAllianceConfModel;
use app\common\model\ShopAllianceModel;
use app\common\model\ShopAllianceNoticeModel;
use app\common\model\ShopBrandModel;
use app\common\model\ShopDetailModel;
use app\common\model\AllianceConf;
use app\common\model\ShopModel;

class Shopalliance extends BaseController{
    public function info(){
        $shop_id=$this->request->shopId;
        if(empty($shop_id)){
            return $this->error('商家ID错误');
        }
        $where['shop_id']=$shop_id;
        $res_data=ShopAllianceModel::info($where);
        return $this->success($res_data);
    }
    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $shop_id=$this->request->shopId;
        $where=[];
        $order['a.id']='desc';
        $where['parent_shop_id']=$shop_id;
        $res_data=ShopAllianceModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('联盟ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=ShopAllianceModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $res=ShopAllianceModel::up(['is_delete'=>$is_delete],$where);
        if(!$res){
            return $this->error('删除失败');
        }
        if($info['parent_shop_id']>0){
            $ShopAllianceModel  = new ShopAllianceModel();
            $parent_shop_id=$info['parent_shop_id'];
            $p_w['shop_id']=$parent_shop_id;
            $p_w['parent_shop_id']=0;
            $p_w['is_delete']=0;
            $ShopAllianceModel->dec($p_w,'shop_num',1);//盟主的联盟商家数-1
        }
        //修改门店状态
        ShopModel::up(['is_alliance'=>0],['shop_id'=>$info['shop_id']]);
        //修改联盟配置状态
        ShopAllianceConfModel::up(['is_delete'=>1],['alliance_id'=>$id]);
        return $this->success();
    }
    public function conf_edit(){
        $shop_id=$this->request->shopId;
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('联盟ID错误');
        }
        $is_shareholder = $this->request->param('is_shareholder',null);
        $is_member=$this->request->param('is_member',null);
        $is_pushflow=$this->request->param('is_pushflow',null);
        $commsion_scale=$this->request->param('commsion_scale',null);
        $give_rights=$this->request->param('give_rights',null);
        $discount=$this->request->param('discount',null);
        $discount_type=$this->request->param('discount_type',null);
        $good_ids=$this->request->param('good_ids',null);
        $discount_time=$this->request->param('discount_time',null);
        $time_slot=$this->request->param('time_slot',null);
        $up=[];
        if($is_shareholder!=null) $up['is_shareholder']=$is_shareholder;
        if($is_member!=null) $up['is_member']=$is_member;
        if($is_pushflow!=null) $up['is_pushflow']=$is_pushflow;
        if($commsion_scale!=null) $up['commsion_scale']=$commsion_scale;
        if($give_rights!=null) $up['give_rights']=$give_rights;
        if($discount!=null) $up['discount']=$discount;
        if($discount_type!=null) $up['discount_type']=$discount_type;
        if($good_ids!=null) $up['good_ids']=$good_ids;
        if($discount_time!=null) $up['discount_time']=$discount_time;
        if($time_slot!=null) $up['time_slot']=$time_slot;
        $res_data=ShopAllianceConfModel::up($up,['alliance_id'=>$id]);
        return $this->success($res_data);
    }
    public function shop_list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $shop_id=$this->request->shopId;
        $name = $this->request->param('name');
        $where=[];
        $order['a.add_time']='desc';
        if(!empty($name)){
            $where['shop_name']=$name;
        }
        $where['is_alliance']=[0,1,2];
        $res_data=ShopModel::shop_list($where,$order,$page,$limit);
        if(!empty($res_data['list'])){
            foreach ($res_data['list'] as &$v){
                $where['parent_shop_id']=$shop_id;
                $where['shop_id']=$v['shop_id'];
                $where['status']=0;
                $info=ShopAllianceModel::info($where);
                $v['is_invite']=0;
                if(!empty($info)){
                    $v['is_invite']=1;
                }
            }

        }
        return $this->success($res_data);
    }
    public function add(){
        $shop_id = $this->request->param('shop_id',0);
        $pay_type = $this->request->param('pay_type',3);
        $parent_shop_id=$this->request->param('parent_shop_id',0);
        $pay_remark=$this->request->param('pay_remark','');
        $pay_img=$this->request->param('pay_img','');
        $upper_num=$this->request->param('upper_num',0);
        $is_tuodian=$this->request->param('is_tuodian',0);
        $tuodian_takes=$this->request->param('tuodian_takes',0);
        $is_flow=$this->request->param('is_flow',0);
        $flow_takes=$this->request->param('flow_takes',0);
        $start_time=$this->request->param('start_time','');
        $live_num=$this->request->param('live_num',0);
        if($shop_id==0) return $this->error('商铺id参数错误');
        $ShopModel= new ShopModel();
        $shop_info=$ShopModel->find(['shop_id'=>$shop_id]);
        if(empty($shop_info)) return $this->error('商铺不存在');
        $ShopAllianceModel= new ShopAllianceModel();
        $s=[];
        $s[]=['status','<>',2];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['is_delete','=',0];
        $all_info=$ShopAllianceModel->where($s)->find();
        if(!empty($all_info)) return $this->error('该商铺已加入联盟或被其他盟主邀请');
        if($start_time=='') $start_time=date('Y-m-d');
        $all_conf=AllianceConf::info();
        if(!empty($all_conf)){
            if($tuodian_takes==0) $tuodian_takes=$all_conf['tuodian_takes'];
            if($flow_takes==0) $flow_takes=$all_conf['flow_takes'];
            if($upper_num==0) $upper_num=$all_conf['shop_num'];
        }
        $insert=compact('shop_id','pay_type','parent_shop_id','pay_remark','pay_img','upper_num','is_tuodian','tuodian_takes','is_flow','flow_takes','start_time','live_num');
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=ShopAllianceModel::insertGetId($insert);
        if($insert_id){
            $alliance_conf=[];
            $alliance_conf['shop_id']=$shop_id;
            $alliance_conf['alliance_id']=$insert_id;
            $alliance_conf['is_shareholder']=$all_conf['is_shareholder'];
            $alliance_conf['is_member']=$all_conf['is_member'];
            $alliance_conf['is_pushflow']=$all_conf['is_pushflow'];
            $alliance_conf['add_time']=date('Y-m-d H:i:s');
            $alliance_conf['edit_time']=date('Y-m-d H:i:s');
            ShopAllianceConfModel::insert($alliance_conf);
            //修改门店状态
            ShopModel::up(['is_alliance'=>2],['shop_id'=>$shop_id]);
        }
        return $this->success($insert_id);
    }
    public function notice_list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $shop_id=$this->request->shopId;
        $where=[];
        $order['a.id']='desc';
        $where['parent_shop_id']=$shop_id;
        $res_data=ShopAllianceNoticeModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function notice_info(){
        $id = $this->request->param('id',0);
        if(empty($id)){
            return $this->error('主键ID错误');
        }
        $where['id']=$id;
        $res_data=ShopAllianceNoticeModel::info($where);
        return $this->success($res_data);
    }
    public function notice_up(){
        $id = $this->request->param('id',0);
        $status = $this->request->param('status',0);
        $examine_remark = $this->request->param('examine_remark','');
        if(empty($id)){
            return $this->error('主键ID错误');
        }
        $up=[];
        if(!empty($status)) $up['status']=$status;
        if(!empty($examine_remark)) $up['examine_remark']=$examine_remark;
        $where['id']=$id;
        $info=ShopAllianceNoticeModel::info($where);
        if(empty($info)){
            return $this->error('数据错误');
        }
        if(!empty($up)){
            $res=ShopAllianceNoticeModel::up($up,['id'=>$id]);
            if($res){
                if($info['type']==1){//入盟申请
                    if($up['status']==1){//审核通过
                        if($info['parent_shop_id']>0){
                            $ShopAllianceModel  = new ShopAllianceModel();
                            $parent_shop_id=$info['parent_shop_id'];
                            $p_w['shop_id']=$parent_shop_id;
                            $p_w['parent_shop_id']=0;
                            $p_w['is_delete']=0;
                            $ShopAllianceModel->inc($p_w,'shop_num',1);//盟主的联盟商家数+1
                        }
                        //修改门店状态
                        ShopModel::up(['is_alliance'=>1],['shop_id'=>$info['shop_id']]);
                    }elseif($up['status']==2){//拒绝
                        //修改门店状态
                        ShopModel::up(['is_alliance'=>0],['shop_id'=>$info['shop_id']]);
                    }
                }elseif ($info['type']==2){//退盟申请
                    if($up['status']==1){//审核通过
                        if($info['parent_shop_id']>0){
                            $ShopAllianceModel  = new ShopAllianceModel();
                            $parent_shop_id=$info['parent_shop_id'];
                            $p_w['shop_id']=$parent_shop_id;
                            $p_w['parent_shop_id']=0;
                            $p_w['is_delete']=0;
                            $ShopAllianceModel->dec($p_w,'shop_num',1);//盟主的联盟商家数-1
                        }
                        //修改门店状态
                        ShopModel::up(['is_alliance'=>0],['shop_id'=>$info['shop_id']]);
                    }
                }

            }
        }
        return $this->success($res);
    }
    public function notice_in(){
        $shop_id=$this->request->shopId;
        $type = $this->request->param('type',1);
        $reason = $this->request->param('reason','');
        $where=[];
        $where['shop_id']=$shop_id;
        $where['status']=1;
        $a_info=ShopAllianceModel::info($where);
        $insert=[];
        if(!empty($type)) $insert['type']=$type;
        if(!empty($reason)) $insert['reason']=$reason;
        if(empty($a_info)){
            return $this->error('数据错误');
        }
        $insert['shop_id']=$shop_id;
        $insert['alliance_id']=$a_info['id'];
        $insert['parent_shop_id']=$a_info['parent_shop_id'];
        if(!empty($insert)){
            $insert['create_time']=date('Y-m-d H:i:s');
            $res=ShopAllianceNoticeModel::insertGetId($insert);
        }
        return $this->success($res);
    }
}

